package com.example.demo.util;

import com.example.demo.po.TBizAuction;
import com.example.demo.po.TBizAuctionRecord;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 拍卖行某个拍卖会的某个作品拍卖缓存
 */
public class AcutionCache {

    public static Map<String,List<TBizAuctionRecord>> codeMap =new ConcurrentHashMap<>();
    public static ReadWriteLock rwLock = new ReentrantReadWriteLock();
    public static Map<String,Integer> anumberMap =new ConcurrentHashMap <>();//围观人数

    public Integer getAnumberMap(String key){
        return  anumberMap.get(key);
    }

    public void setAnumberMap(String key ,Integer nu){
        anumberMap.put(key,nu);
    }

    /**
     * 创建
     * @param ar
     */
    public  void  init(TBizAuctionRecord ar){
        System.out.println(codeMap.get("aucName")+" 拍卖会开始 "+DateUtil.formatDate(new Date()));
        try {
            // 开启 写锁
            rwLock.writeLock().lock();
            List<TBizAuctionRecord> list  = new ArrayList<>();
            list.add(ar);
            codeMap.put(ar.getAuctionId()+"_"+ar.getArtCode(),list);
            // 释放写锁
            rwLock.writeLock().unlock();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 读取最新的出价
     * @param key
     * @return
     */
    public  TBizAuctionRecord getLastInfo(String key){
        // 开启读锁
        rwLock.readLock().lock();
        TBizAuctionRecord ar  = new TBizAuctionRecord();
        try {
            List<TBizAuctionRecord> list = codeMap.get(key);
            int i = list.size();
            ar = list.get((i-1));
            // 关闭读锁
            rwLock.readLock().unlock();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ar;
    }

    /**
     * 读取
     * @param key
     * @return
     */
    public    List<TBizAuctionRecord>  getCodeMap(String key){
        // 开启读锁
        rwLock.readLock().lock();
        List<TBizAuctionRecord> list  = new ArrayList<>();
        try {
           list = codeMap.get(key);
        }catch (Exception e){
            e.printStackTrace();
        }
        // 关闭读锁
        rwLock.readLock().unlock();
        return list;
    }



    /**
     * 更新
     * @param key
     * @param map
     */
    public  boolean setCodeMap(String key ,TBizAuctionRecord map){
        try {
            boolean result = true;
            // 开启读锁
            rwLock.readLock().lock();
            System.out.println("*****key*****"+key);
            List<TBizAuctionRecord> list = codeMap.get(key);
            if(list==null||list.size()==0)
                list = new ArrayList<>();
            else{
                //校验 然后排序
                for(TBizAuctionRecord ar :list){
                    Double price = Double.valueOf(ar.getNewPrice());
                    Double newPrice = Double.valueOf(map.getNewPrice());
                    System.out.println("*******price*********"+price);
                    System.out.println("*******newPrice*********"+newPrice);
                    if(price>=newPrice){
                        System.out.println("你的出价低于最新出价");
                        result=false;
                        break;
                    }
                }
            }
            // 关闭读锁
            rwLock.readLock().unlock();
            if(result){
                // 开启 写锁
                rwLock.writeLock().lock();
                list.add(map);
                codeMap.put(key,list);
                // 释放写锁
                rwLock.writeLock().unlock();
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }


    /**
     * 销毁
     */
    public  void  remove(String key,String name){
        System.out.println(name+"********拍卖会结束******* "+DateUtil.formatDate(new Date()));
        try {
            // 开启 写锁
            rwLock.writeLock().lock();
            System.out.println(key+" 的出价记录删除");
            codeMap.remove(key);
            // 释放写锁
            rwLock.writeLock().unlock();
        }catch (Exception e){
            e.printStackTrace();
            rwLock.writeLock().unlock();
        }
    }


}
